const state = {
    encoder: new TextEncoder(),
    decoder: new TextDecoder(),
  };

import {
    LocalParticipant,
  } from 'livekit-client';

export interface ChatMessage {
    id: string;
    timestamp: number;
    message: string;
  }
export const DataTopic = {
    MULTI_MODAL_DATA_PLAYING: 'multi_modal_data_playing',
    AGENT_STATE_SYNC: 'agent_state_sync',
    WAKEUP: 'wakeup',
    CHAT: 'lk-chat-topic',
    FLOWNODE_DATA: 'multi_modal_data',
    INTERRUPT: 'INTERRUPT',
    CHAT_UPDATE: 'lk-chat-update-topic',
    VISEME:'viseme',
    STOP_PLAY:'stop_play',
    DISCONTINUE_GENERATE:'discontinue_generate',
    ERROR:'error',
    STT:'STT' ,//asr结果
  } as const;

export const DataCommand={
    AGENT_STATE_SYNC: 'agent_state_sync',
    WAKEUP: 'wakeup',
    VISEME_ACK: 'viseme_ack',
    MUTI_MODAL_METADATA:'multi_modal_metadata'
} as const

export async function sendMessage(localParticipant: LocalParticipant,jmessage:any,options:Object,isTopic:boolean){
    
    const timestamp = Date.now();
    
    var message;

    if(typeof(jmessage)==='string'){
        message=jmessage
    }else{
       message= JSON.stringify(jmessage)
    }
    var tringMessage
    if(isTopic){
      tringMessage=message
    }else{
      const id=new Date().getTime().toString();
      const sendMessage={
        timestamp,
        message,
        id
      }
      tringMessage=JSON.stringify(sendMessage);
    }

    
    const msg = state.encoder.encode(tringMessage );
    await localParticipant.publishData(msg,options);
    console.log(tringMessage)
    return tringMessage;
}